﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename :Experience_ESon.aspx.cs
//        description :经验分享详细页面
//
//        created by 邹兴武 at  2010-12-30
//
//======================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.hgmanage.Share
{
    public partial class Experience_ESon : System.Web.UI.Page
    {
        private readonly D_T_Experience_Exp _dal = new D_T_Experience_Exp();
        protected void Page_Load(object sender, EventArgs e)
        {
            //用来初始化用户的按钮,执行此语句的时候,but并没有具体产生
            WebUserToolBar1.EditInit(
               UserConst.Exp_GetUser,
               UserConst.Exp_GetButtonList,
               SetBut,
              UserConst.Exp_ToolBarErr
            );
            if (!IsPostBack)
            {
                if (Request.Params["id"] != null)
                {
                    ViewState.Add("id", Request.Params["id"]);
                    InitData(Request.Params["id"].Exp_IntTryParse());
                }
            }
        }

        /// <summary>
        /// 初始化数据
        /// </summary>
        private void InitData(int id)
        {
            var model = _dal.GetModel(id);
            if (model == null)
            {
                var SKey = this.WebUserToolBar1.EditSKey;
                this.Exp_PageAlterHref("没有找到相应的资料!", "experience.aspx?" + SKey);
                return;
            }
            this.txtType.Text = new D_T_Category_Exp().GetModel(model.CateID).CateName;
            this.txtName.Text = model.Title;
            this.txtSource.Text = model.Source;
            this.txtMember.Text = new D_T_Member().GetModel(model.MemberID).Name;
            this.txtAddTime.Text = model.UploadDate.ToString("yyyy-MM-dd");
            this.txtPic.Text = model.Pic;
            this.Image1.ImageUrl = model.Pic;
            this.rblTopis.Text = model.Topis.Exp_Trim();
            this.rblRem.Text = model.RemSign ? "1" : "0";
            this.txtWebTitle.Text = model.WebTitle.Exp_Trim();
            this.txtWebKey.Text = model.WebKey.Exp_Trim();
            this.txtWebSummary.Text = model.WebSummary.Exp_Trim();
            this.litContent.Text = model.Contents;

        }

        private void SetBut(int i, Button but)
        {
            switch (i)
            {
                case 1:
                    but.CausesValidation = false;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 保存按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button0(object sender, EventArgs e)
        {
            AdminPublic.Try(() =>
            {
                var model = CreateModel();
                Log_Type le = Log_Type.update;
                int iRet = 0;
                if (model.id > 0)
                {
                    iRet = _dal.Update(model);
                }

                if (iRet > 0)
                {
                    //写日志
                    this.Exp_PageWriteLog(le, "经验分享信息", iRet);
                    var SKey = this.WebUserToolBar1.EditSKey;
                    this.Exp_PageAlterHref("操作成功", "experience.aspx?" + SKey);
                }
                else
                {
                    this.Exp_PageAlter("操作失败");
                }
            });
        }

        /// <summary>
        /// 获取model对象
        /// </summary>
        private M_T_Experience CreateModel()
        {
            var id = ViewState["id"].Exp_IntTryParse();
            var model = _dal.GetModel(id);
            model.Topis = this.rblTopis.Text.Exp_IntTryParse(); //this.txtTopis.Text.Exp_IntTryParse();
            model.RemSign = this.rblRem.Text == "1";
            model.WebTitle = this.txtWebTitle.Text.Exp_Trim();
            model.WebKey = this.txtWebKey.Text.Exp_Trim();
            model.WebSummary = this.txtWebSummary.Text.Exp_Trim();
            model.UserID = this.Exp_GetUser().id;
            return model;
        }

        /// <summary>
        /// 返回按钮事件
        /// </summary>
        protected void WebUserToolBar1_Button1(object sender, EventArgs e)
        {
            var SKey = this.WebUserToolBar1.EditSKey;
            Response.Redirect("experience.aspx?" + SKey);
        }
    }
}
